<?php
/**
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file license.txt or that can be found at 
 * http://code.google.com/p/billboardce/wiki/License.
 *
 * @package    Slide_Option_Schedule_Module
 * @category   Model
 * @license    http://code.google.com/p/billboardce/wiki/License New BSD License
 * @version    SVN: $Id: $
 */
class Slide_Option_Schedule_Module implements Slide_Option_Interface
{
    public function getTitle()
    {
        return 'Schedule';
    }
    
    public function getOptionForm($slideId)
    {
        $schedule = new Slide_Option_Schedule_Data();
        
        $data = $schedule->find($slideId);
        if (is_null($data)) {
            $data = array(
                'slideId' => $slideId,
            );
        } else {
            $data = $data->toArray();
        }
        
        return $schedule->form($data)->__toString();
    }
    
    public function getData($slideId)
    {
        
        $schedule = new Slide_Option_Schedule_Data();
        
        $data = $schedule->find($slideId);
        if (is_null($data)) {
            return array();
        } else {
            
            $retArray = array();
            
            $retArray['fromDate']     = strftime('%m/%d/%Y', $data->fromDt);
            $retArray['fromHour']     = strftime('%l', $data->fromDt);
            $retArray['fromMinute']   = strftime('%M', $data->fromDt);
            $retArray['fromMeridian'] = strftime('%p', $data->fromDt);
            
            $retArray['toDate']       = strftime('%m/%d/%Y', $data->toDt);
            $retArray['toHour']       = strftime('%l', $data->toDt);
            $retArray['toMinute']     = strftime('%M', $data->toDt);
            $retArray['toMeridian']   = strftime('%p', $data->toDt);
            
            return $retArray;
        }
    }
    
    public function saveData($slideId, array $data)
    {
        $schedule = new Slide_Option_Schedule_Data();
        
        $existingData = $schedule->find($slideId);
        
        $data = array(
            'slideId' => $slideId,
            'fromDt'  => strtotime($data['fromDate'] . ' ' . $data['fromHour'] . ':' . $data['fromMinute'] . ' ' . $data['fromMeridian']),
            'toDt'    => strtotime($data['toDate'] . ' ' . $data['toHour'] . ':' . $data['toMinute'] . ' ' . $data['toMeridian'])
        );
        
        if (is_null($existingData)) {
            $schedule->insert($data);
        } else {
            $schedule->update($data, null);
        }            
    }
    
    public function saveOptionForm($slideId, array $data)
    {
        $schedule = new Slide_Option_Schedule_Data();
        
        $form = $schedule->form(array('slideId' => $slideId));
        
        if ($form->isValid($data)) {
            $existingData = $schedule->find($slideId);

            if (is_null($existingData)) {
                $schedule->insert($data);
            } else {
                $data['slideId'] = $slideId;
                $schedule->update($data, null);
            }
        }
        
        return $form->getErrorMessages();
    }
}